package com.sise.parkinglot.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

/**
 * @author ZJ
 */
public class JwtUtil {

    public static final String SING = "zBjVnxIx2Sy#YiYwZdUE";

    public static String createToken(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.YEAR, 1);

        JWTCreator.Builder builder = JWT.create();

        map.forEach(builder::withClaim);

        return builder.withExpiresAt(instance.getTime())
                .sign(Algorithm.HMAC256(SING));
    }

    public static DecodedJWT verify(String token) {
        return JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
    }

}
